package com.bw.zonghe0719.Login

import android.content.Intent
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.mvvm.BaseActivity
import com.bw.zonghe0719.R
import com.bw.zonghe0719.databinding.ActivityUpdateBinding
@Route(path = "/app/Login/UpdateActivity")
class UpdateActivity : BaseActivity<ActivityUpdateBinding,LoginViewModel>() {
    override fun initView() {
        var phone = SPUtils.getInstance().getString("phone")
        binding.tvPhone.text="当前手机号:${phone.replaceRange(3,7,"****")}"
        //发送验证码
        binding.getyzm.setOnClickListener {
            viewModel.codeSend(phone)
        }
        //点击按钮修改密码
        binding.update.setOnClickListener {
            val pwd=binding.pwd.text.toString()
            val repwd=binding.rePwd.text.toString()
            val code=binding.etYzm.text.toString()
            if(pwd.isEmpty()||repwd.isEmpty()||code.isEmpty()){
                ToastUtils.showLong("输入框不能为空")
            }else{
                if(pwd.equals(repwd)){
                    viewModel.updatePwd(phone,code,pwd)
                }else{
                    ToastUtils.showLong("两次密码不一致")
                }
            }
        }
    }

    override fun initData() {
        viewModel.codeSendSuccess.observe(this){
            if(it.code==200){
                binding.etYzm.setText(it.data.content.substring(8,14))
            }
        }
        viewModel.updatePwdSuccess.observe(this){
           if(it.code==200){
               startActivity(Intent(this@UpdateActivity,LoginActivity::class.java))
               finish()
           }
            ToastUtils.showLong(it.message)
        }
        viewModel.updatePwdFail.observe(this){
            ToastUtils.showLong(it)
        }
    }

    override fun getViewModel(): Class<LoginViewModel> {
        return LoginViewModel::class.java
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_update
    }
}